<template>
  <el-dialog
    :modal-append-to-body="true"
    @close='closeDialog'
    ref="restartView"
    title="提示"
    :show-close="false"
    :visible.sync="dialogVisible"
    width="30%">
    <span>确定要重启吗？</span>
    <span slot="footer" class="dialog-footer">
        <el-button @click="closeConfirm">取 消</el-button>
        <el-button type="primary" @click="restart">确 定</el-button>
     </span>
  </el-dialog>
</template>

<script>
  import {loading} from 'element-ui'
    export default {
        name: "restartConfirm",
      props:['dialogVisible','newIP'],
      data:function () {
        return{
          loading:false,
          restartStatus:false
        }
      },mounted:function () {

      }
      ,methods:{
          closeConfirm:function(){
            this.$emit('closeDialog',false)
          },
        closeDialog:function(){
          console.log('22');
          this.$emit('closeDialog',false)
        },
        restart:function () {
          var _this=this
          _this.$emit('closeDialog',false)
          // var restartOptIndex=_this.BASE.loading('正在发送重启指令...')

          var restartOptIndex = this.$loading({
            lock: true,
            text: '正在发送重启指令...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          });

          // _this.loading=true
          console.log('确定重启2');
          _this.getAjax({
            params:{
              opt:'device-reboot',
              ajaxTime:new Date().getTime()
            }
          }).then(function (res) {
            restartOptIndex.close()
            console.log('重启回调参数',res);
            var restartLoading = _this.$loading({
              target:'.app-main',
              lock: true,
              text: '正在重启，请稍后...',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.7)'
            });
            var restartInterval=setInterval(function () {
              if(_this.restartStatus){
                sessionStorage.clear()
                _this.tips('重启成功','success')
                console.log('重启成功');
                clearInterval(restartInterval)
                restartLoading.close()
                // _this.tips('重启成功','success')
                if(_this.newIP){
                  location.href="http://"+_this.newIP
                }else{
                  setTimeout(function () {
                    _this.$router.push({ path: '/' })
                  },1000)
                }

              }else{
                _this.getrebootstatus()
              }

            },2000)
          }).catch(function (err) {
            console.log(err);
          }).finally(function (fn) {
            console.log(fn);
          })
        },
        getrebootstatus:function () {
          var _this=this
          _this.postAjax({
            method:'getrebootstatus',
            params:{},
            newIP:_this.newIP?'http://'+_this.newIP+'/LadderInspection':_this.BASE.interfaceAddress
          }).then(function (res) {
            console.log(res);
            if(res.params.rebootstatus==1){
              sessionStorage.clear()
              setTimeout(function () {
                _this.restartStatus=true
              },2000)
            }else{
              // _this.tips(res.errorInfo.detail,'error')
            }
          }).catch(function (err) {
            console.log(err);
          }).finally(function (fn) {
            console.log(fn);
          })
        }
      }
    }
</script>

<style scoped>

</style>
